package com.example.springcloud.controller;

import com.example.springcloud.common.ResponseEntity;
import com.example.springcloud.common.entity.Dept;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 莫磊
 * @date 2025/8/20
 * @description
 */
@RestController
@RequestMapping("/consumer/dept")
public class DeptConsumerForRibbonController {

    @Resource
    private RestTemplate restTemplate;

    /**
     * 指定dept服务的服务名（spring.application.name）
     */
    private String serviceName = "DEPT-SERVICE";

    @GetMapping("/getById/{id}")
    @Operation(summary = "根据id查询部门", description = "返回指定id的部门", responses = {
            @ApiResponse(
                    description = "成功获取到部门信息", responseCode = "200",
                    content = @Content(schema = @Schema(implementation = Dept.class))
            )
    }
    )
    public ResponseEntity<Dept> getById(@PathVariable("id") Long id) {
        ResponseEntity forObject = restTemplate.getForObject("http://" + serviceName + "/dept/getById/" + id, ResponseEntity.class);
        return forObject;
    }

    @PostMapping("/addDept")
    @Operation(summary = "添加部门")
    public ResponseEntity<Dept> addDept(Dept dept) {
        ResponseEntity responseEntity = restTemplate.postForObject("http://" + serviceName + "/dept/addDept", dept, ResponseEntity.class);
        return responseEntity;
    }

    @Operation(summary = "根据部门名称模糊查询部门",
            parameters = @Parameter(name = "dname", description = "部门名称"))
    @GetMapping("/findByDnameLike/{dname}")
    public ResponseEntity<List<Dept>> findByDnameLike(@PathVariable("dname") String dname) {
        ResponseEntity<List<Dept>> response = restTemplate.getForObject("http://" + serviceName + "/dept/findByDnameLike/" + dname, ResponseEntity.class);
        return response;
    }

    @GetMapping("/findByDeptno/{deptno}")
    @Operation(summary = "根据部门编号查询部门",
            parameters = @Parameter(name = "deptno", description = "部门编号"))
    public ResponseEntity<List<Dept>> findByDeptno(@PathVariable("deptno") String deptno) {
        ResponseEntity response = restTemplate.getForObject("http://" + serviceName + "/dept/findByDeptno/" + deptno, ResponseEntity.class);
        return response;
    }

}
